
#import "VPTabBar.h"

#pragma mark ---

@interface VPTabBar ()

@end

#pragma mark ---

@implementation VPTabBar

#pragma mark ---

-(id) initWithPosY:(CGFloat) ypos btnList:(NSArray*) btnlist
{
    self = [super initWithFrame:CGRectMake(0, ypos, kScreenWidth, MainTabBar_H)];
    
    if (self)
    {
        _btnList = btnlist;
        self.backgroundColor = [UIColor clearColor];
    }
    
    return self;
}

-(void)initMyContent
{
    if(_bgImage)
    {
        UIImageView* bgView = [[UIImageView alloc] initWithFrame:self.bounds];
        bgView.image = _bgImage;
        [self addSubview:bgView];

    }
    
    NSInteger count = _btnList.count;
    CGFloat btnWidth = self.width/count;
    
    for(NSInteger index=0; index<count; ++index)
    {
        CGFloat x = index*btnWidth;
        id<VPTBarBtnInterface> item = [_btnList objectAtIndex:index];
        [item initMyContentWithFrame:CGRectMake(x, 0, btnWidth, self.height)];
        
        if ([item isKindOfClass:[UIButton class]])
        {
            UIButton<VPTBarBtnInterface>* btn = (UIButton<VPTBarBtnInterface>*)item;
            btn.tag = index;
            [btn addTarget:self action:@selector(itemBtnClicked:) forControlEvents:UIControlEventTouchDown];
            
//            UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleClickedTapGesture:)];
//            tapGesture.numberOfTapsRequired=2;
//            [btn addGestureRecognizer:tapGesture];
            
            if(0==index)
                item.isSelectedMine = YES;
            else
                item.isSelectedMine = NO;
            
            [self addSubview:btn];
        }
    }
}

- (void)selectTabAtIndex:(NSInteger)index{
    if(index < 0 || index >= [_btnList count]){
        return;
    }
    for(int i =0;i<[_btnList count];i++){
        id<VPTBarBtnInterface> item = [_btnList objectAtIndex:i];
        if ([item isKindOfClass:[UIButton class]])
        {
            if(i == index){
                item.isSelectedMine = YES;
            }else{
                item.isSelectedMine = NO;
            }
        }
    }
}



-(void) itemBtnClicked:(UIButton<VPTBarBtnInterface>*) sender
{
    if (sender.isSelectedMine)
    {
        if(_mydelegate && [_mydelegate respondsToSelector:@selector(doubleClickedAtIndex:)])
            [_mydelegate doubleClickedAtIndex:sender.tag];
        
        return;
    }
    
    sender.isSelectedMine = YES;
    for(id<VPTBarBtnInterface> item in _btnList)
    {
        if(item != sender)
            item.isSelectedMine = NO;
    }
    
    if(_mydelegate && [_mydelegate respondsToSelector:@selector(selectBarBtnAtIndex:)])
        [_mydelegate selectBarBtnAtIndex:sender.tag];
}


@end
